﻿#ifndef FIREWORKBASE_H
#define FIREWORKBASE_H

#include <random>
#include <functional>

#include "FireworkParticle.h"

enum class FireworkState
{
    Flying,
    Blast,
    Done
};

using ColorAndSpeedlist = std::map<vsg::vec4, std::vector<vsg::dvec3>>;

class Firework
{
public:
    Firework(vsg::dvec3 pos, float speed, uint32_t count) : m_pos{ pos }, z_speed{ speed }, m_fireworkParticlesCount{ count }
    {
        std::random_device rd;  // 将用于获得随机数引擎的种子
        m_gen = std::mt19937_64(rd()); // 以 rd() 播种的标准 mersenne_twister_engine
    }
    ~Firework() {};

    void init();
    void update(const std::chrono::nanoseconds& delt);
    vsg::ref_ptr<vsg::Node> getNode();

    bool isDone()
    {
        return m_state == FireworkState::Done;
    };

    void reset(vsg::dvec3 pos, double speed = 60);

    void activate();

private:
    void addNewBombs(vsg::dvec3 pos, const std::vector<vsg::dvec3>& speedList, vsg::vec4 color, uint8_t lengthBomb);
    void invalid();
    void initNode();
    void initBombFuncs();
    void setBombsMovePara(int begin, int end);
    void initBombRandom();
private:
    std::vector<FireworkParticle> m_fireworkParticles;
    vsg::dvec3 m_pos;
    std::mt19937_64 m_gen;

    FireworkState m_state{ FireworkState::Flying };
    double z_speed;
    vsg::ref_ptr<vsg::MatrixTransform> m_matrixTransform;
    vsg::ref_ptr<vsg::Switch> m_switch;
    uint32_t m_fireworkParticlesCount;
    uint32_t m_activateFireworkParticlesCount{ 0 };
    std::vector<std::function<ColorAndSpeedlist()>> m_initBombFuncs;
};

#endif
